package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;

import com.example.myapplication.databinding.ActivityMainBinding;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.btn.setOnClickListener(v -> startActivity(new Intent(MainActivity.this,MainActivity2.class)));
        Test<User> test = new Test<>();
        test.get("http://192.168.43.102:8080/book/t",User.class);
        test.liveData.observe(MainActivity.this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                binding.btn.setText(user.getData().toString());
            }
        });
        binding.btn.setOnClickListener(v -> test());
    }
    private void test() {
        Test<User> userTest = new Test<>();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id","1");
            jsonObject.put("name","吉祥物");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        userTest.post("http://192.168.43.102:8080/save",User.class,jsonObject);
        userTest.liveData.observe(MainActivity.this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                binding.btn.setText(user.getData().toString());
            }
        });
    }
}